﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;

namespace School.Admission
{
    public class Test_Teacher
    {
        
        private static SchoolEntities db = new SchoolEntities();
        
        public static Int64 Add(ADM_Test_Teacher  Test_Teacher)
        {
            try
            {
                db.AddToADM_Test_Teacher(Test_Teacher);

                db.SaveChanges();
                return Test_Teacher.Id;
            }
            catch
            {
                return -1;
            }
        }
        public static ADM_Test_Teacher Single(long id)
        {
            try
            {
                return db.ADM_Test_Teacher.Single(x => x.Id == id);
            }
            catch
            {
                return null;
            }
        }


        public static IEnumerable<object> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from TestTeacher in db.ADM_Test_Teacher
                        select TestTeacher;
            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Test_Id")
                {
                    long Test_Id;
                    Test_Id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Test_Id == Test_Id);
                }
                if (item.Key == "Teacher_Id")
                {
                    long Teacher_Id;
                    Teacher_Id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Teacher_Id == Teacher_Id);
                }
                
            }
            return query.ToList();
        }

        public static void Update(ADM_Test_Teacher  TestTeacher)
        {

            ADM_Test_Teacher c = db.ADM_Test_Teacher.Single(x => x.Id == TestTeacher.Id);
            c.Test_Id = TestTeacher.Test_Id;
            c.Teacher_Id = TestTeacher.Teacher_Id;

            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.ADM_Test_Teacher.DeleteObject(db.ADM_Test_Teacher.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IQueryable <object > GetTeachers()
        {
            var result = from c in db.ORG_Stuff_Teacher  
                         from x in db.ORG_Stuff 
                         where c.Stuff_Id == x.Id 
                         select new {
                         Id = c.Id,
                         Name = x.FName +" " + x.MName + " " +  x.LName 
                         } ;
            return result;
        }
        public static IQueryable<object> GetTestTeachers(int TestId)
        {
            var result = from c in db.ORG_Stuff_Teacher
                         from x in db.ORG_Stuff
                         from z in db.ADM_Test_Teacher 
                         where c.Stuff_Id == x.Id 
                         where z.Teacher_Id == c.Id 
                         where z.Test_Id == TestId 
                         select new
                         {
                             Id = c.Id,
                             Name = x.FName + " " + x.MName + " " + x.LName
                         };
            return result;
        }
        public static IQueryable<object> GetTestAvailableTeachers(int TestId)
        {
            var result = from c in db.ORG_Stuff_Teacher
                         from x in db.ORG_Stuff
                         where c.Stuff_Id == x.Id
                         select new
                         {
                             Id = c.Id,
                             Name = x.FName + " " + x.MName + " " + x.LName
                         };

            var selected = from c in db.ORG_Stuff_Teacher
                           from z in db.ADM_Test_Teacher
                           from x in db.ORG_Stuff
                           where z.Teacher_Id == c.Id
                           where z.Test_Id == TestId
                           select new
                           {
                               Id = c.Id,
                               Name = x.FName + " " + x.MName + " " + x.LName
                           };
            result = result.Except(selected);
            return result;
        }
    }
    
}
